
/**
 * @class Kohive.controllers.AppsController
 * @extends ExtMVC.Controller
 * Manages applications on Kohive
 */
Kohive.controllers.AppsController = function() {
	// Get the OS and the controller
	var os = ExtMVC.OS.getOS();
	var controller = this;

	Kohive.controllers.AppsController.superclass.constructor.call(this);

	/**
	 * @action destroy 
	 * Action which attempts to destroy an application
	 */
	this.registerAction('destroy', function(app) {
		// Check if the application + application id has been set, as if they haven't we cannot
		// destroy the application
		if (!app || !app.id) {
			return;
		};

		// Create a new instance of an application with the correct Id
		var application = new Kohive.models.Application(app);

		// Create the function
		var destroyFn = function(msg) {
			// Check if the user selected remove (yes btn in ext)
			if (msg == "yes") {
				application.destroy({
							url		: '/memberships/'
									+ os.getCurrentHive().get('id')
									+ '/applications/' + app.id,
							success	: function() {

							},
							failure	: function(errors) {
								Ext.Msg
										.alert(
												'delete_application',
												'306:'
														+ Ext.ux.Translator
																.translate('something_wrong')
										);
							}
						}
				);
			};
		};

		// Show the Ext.Msg to allow the user to either remove to keep the application
		return Ext.Msg.show({
					title	: Ext.ux.Translator.translate(''),
					msg		: Ext.ux.Translator.translate(
							'delete_application_desc', {
								applcation_name	: app.text || ""
							}
					),
					buttons	: {
						yes	: Ext.ux.Translator.translate('remove'),
						no	: Ext.ux.Translator.translate('keep')
					},
					fn		: destroyFn
				});
	}
	);
};

Ext.extend(Kohive.controllers.AppsController, ExtMVC.Controller);

ExtMVC.OS.getOS().registerController('apps', Kohive.controllers.AppsController);
